﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace ReadNovel
{
    public class GloabalData : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        private ReadNovel.API.DefaultClient client;
        public ReadNovel.API.DefaultClient Client
        {
            get
            {
                if (client == null)
                    client = new ReadNovel.API.DefaultClient();
                return client;
            }
        }

        private static GloabalData instance;
        public static GloabalData Instance
        {
            get
            {
                //if (instance == null)
                //    instance = new GloabalData();
                return instance;
            }
        }
        private void OnPropertyChanged(string name)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
        public GloabalData()
        {
            if (instance != null)
                throw new Exception("只能有一个实例");//之所以这么做，是因为需要配合绑定
            instance = this;
        }



        private ReadNovel.API.Domain.UserInfo userInfo;
        /// <summary>
        /// 登录的用户信息
        /// </summary>
        public ReadNovel.API.Domain.UserInfo UserInfo
        {
            get
            {
                if (userInfo == null)
                {
                    if (System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Contains("UserInfo"))
                        userInfo = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["UserInfo"] as ReadNovel.API.Domain.UserInfo;
                }
                return userInfo;
            }
            set
            {
                if (userInfo != value)
                {
                    userInfo = value;
                    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["UserInfo"] = value;
                    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Save();
                    this.OnPropertyChanged("UserInfo");
                }
            }
        }

        public bool IsLogin
        {
            get
            {
                return UserInfo != null;
            }
        }
    }
}
